package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.DirectSaleOrderService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.OffsetDateTime

@RestController
@RequestMapping("directSaleClients/orders")
class DirectSaleOrderController(
    private val directSaleOrderService: DirectSaleOrderService,
) {

    class QueryDirectSaleOrderRequest(
        val clientIds: List<Int> = emptyList(),
        val startDate: LocalDate?,
        val endDate: LocalDate?,
        val createdByIds: List<Int> = emptyList(),
    )

    @HasAuthority(Authority.卖场_订单_查询)
    @GetMapping
    fun queryDirectSaleOrder(queryDirectSaleOrderRequest: QueryDirectSaleOrderRequest, pageable: Pageable):Page<QueryDirectSaleOrderResponse> {
        return this.directSaleOrderService.queryDirectSaleOrder(queryDirectSaleOrderRequest,pageable)
    }

    data class QueryDirectSaleOrderResponse(
        val id:Int,
        val createdBy: IntIdAndName,
        val createdDateTime: OffsetDateTime,
        val goodsKindAmount:Int,
        val client:IntIdAndName,
        val submittedDateTime: OffsetDateTime?,
        val paidPrice: BigDecimal,
        val shouldPaidPrice:BigDecimal,
    )

    data class UpdateOrderGoodsRequest(
        val goodsList:List<OrderGoodsItem>
    )

    data class OrderGoodsItem(
        val id:Int,
        val amount:Int
    )

    @HasAuthority(Authority.卖场_订单_下单)
    @PatchMapping("{id}/goods")
    fun updateOrderGoods(@RequestBody updateOrderGoodsRequest: UpdateOrderGoodsRequest, @PathVariable id: Int,
    ){
        return this.directSaleOrderService.updateOrderGoods(id,updateOrderGoodsRequest)
    }

    @HasAuthority(Authority.卖场_订单_查询)
    @GetMapping("{id}")
    fun getDirectSaleOrderDetail(@PathVariable id: Int, includeItems:Boolean = false):DirectSaleOrderDetail{
        return this.directSaleOrderService.getDirectSaleOrderDetail(id,includeItems)
    }

    data class DirectSaleOrderDetail(
        val id:Int,
        val createdDateTime: OffsetDateTime,
        val createdBy: IntIdAndName,
        val client:IntIdAndName,
        val items:List<DirectSaleOrderItemResponse>,
        val submitDateTime:OffsetDateTime?,
        val paidTimes: Int,
        val paidPrice: BigDecimal,
        val shouldPaidPrice:BigDecimal,
    )

    data class DirectSaleOrderItemResponse(
        val id:Int,
        val name: String,
        val barcode:String,
        val price: BigDecimal,
        val amount:Int,
        val createdDateTime: OffsetDateTime,
        val lastModifiedDateTime: OffsetDateTime?,
        val brand:IntIdAndName?,
        val category:IntIdAndName?,
        val factory:IntIdAndName?,
        val amountInOrder:Int,
        val boxSpecification:String?
    )

    @HasAuthority(Authority.卖场_订单_查询)
    @GetMapping("employees", params = ["projection=summary"])
    fun getDirectSaleOrderCreatedEmployees():List<IntIdAndName>{
        return this.directSaleOrderService.getAllEmployees()
    }


    @HasAuthority(Authority.卖场_订单_下单)
    @PatchMapping("{id}/submit")
    fun submitOrder(@PathVariable id: Int ){
        return this.directSaleOrderService.submitDirectSaleOrder(id)
    }

    @HasAuthority(Authority.卖场_订单_转化为送货单)
    @PostMapping("{id}/toStore")
    fun delivery(@PathVariable id: Int ):Int{
        return this.directSaleOrderService.toStore(id).id
    }

    data class PayDirectSaleOrderRequest(
        val price: BigDecimal,
    )

    @PostMapping("{id}/pay")
    fun payOrder(@PathVariable id: Int,@RequestBody request: PayDirectSaleOrderRequest){
        return this.directSaleOrderService.pay(id,request)
    }
}
